package org.gavin.core.conf;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.gavin.core.domain.ExpireCache;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * CaffeineConfig
 *
 * @author grl
 * @date 2024/1/25
 */
@Configuration
public class CacheConfig {

    @Bean
    @Qualifier(value = "accessLimitLocalCache")
    public Cache<String, ? extends ExpireCache> accessLimitLocalCache() {
        return Caffeine.newBuilder()
                .expireAfter(new CacheExpiry())
                .initialCapacity(100)
                .maximumSize(1000)
                .build();
    }

    @Bean
    @Qualifier(value = "commonLocalCache")
    public Cache<String, Object> commonLocalCache() {
        return Caffeine.newBuilder()
                .initialCapacity(100)
                .expireAfterWrite(Duration.ofMinutes(10))
                .maximumSize(1000)
                .build();
    }
}
